package com.yugao.fintech.draper.admin.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.draper.admin.convert.UserPostConvert;
import com.yugao.fintech.draper.admin.entity.SysUserPost;
import com.yugao.fintech.draper.admin.mapper.UserPostMapper;
import com.yugao.fintech.draper.admin.param.UserPostDTO;
import com.yugao.fintech.draper.admin.param.UserPostQuery;
import com.yugao.fintech.draper.admin.service.UserPostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class UserPostServiceImpl extends ServiceImpl<UserPostMapper, SysUserPost>  implements UserPostService {
    @Autowired
    private UserPostMapper userPostMapper;

    @Override
    public IPage<UserPostDTO> pageQuery(UserPostQuery pageDTO) {
        Page<SysUserPost> userPostPage = userPostMapper.selectPage(Page.of(pageDTO.getPageNum(), pageDTO.getPageSize()), Wrappers.emptyWrapper());
        return UserPostConvert.INSTANCE.convert(userPostPage);
    }

    @Override
    public Boolean addUserPost(UserPostDTO userPostDto) {
        SysUserPost userPost = UserPostConvert.INSTANCE.convert(userPostDto);
        Collections.singletonList(userPost).forEach(userPostMapper::insert);
        return Boolean.TRUE;
    }

    @Override
    public Boolean updateUserPost(UserPostDTO userPostDto) {
		SysUserPost sysUserPost = UserPostConvert.INSTANCE.convert(userPostDto);
        return userPostMapper.updateById(sysUserPost) > 0;
    }

    @Override
    public UserPostDTO findById(Long id) {
        return UserPostConvert.INSTANCE.convert(userPostMapper.selectById(id));
    }

    @Override
    public List<UserPostDTO> findAll() {
		List<SysUserPost> userPosts = userPostMapper.selectList(Wrappers.emptyWrapper());
        return UserPostConvert.INSTANCE.convert(userPosts);
    }

    @Override
    public Boolean deleteUserPost(Long id) {
        return userPostMapper.deleteById(id) > 0;
    }
}